package com.alien.admin.modules.basic.utils;

import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.model.*;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

/**
 * Copyright (C), 2014-2021, 厦门神州鹰软件科技有限公司
 * FileName: JenkinsServiceUtils
 *
 * @author: zengzf
 * Date:     2021/9/15 9:04
 * Description:
 * History:
 */
public class JenkinsServiceUtils {

    private static String uri = "http://10.1.30.119:8080/basic";
    private static String name = "admin";
    private static String pwd = "123456";

    // 访问api需要特别的token apiToken
    // 注意：apiToken比密码好用：因为有些接口用密码会报没有权限！
    private static String token = "119fafa5af9f5208d3fc35194aa69cf954";


    /**
     * 获取jenkins上的所有job
     *
     * @throws URISyntaxException
     * @throws IOException
     */
    public static Map<String, Job> getJobs() throws URISyntaxException, IOException {
        JenkinsServer jenkins = new JenkinsServer(new URI(uri), name, pwd);
        Map<String, Job> jobs = jenkins.getJobs();
        return jobs;
    }


    /**
     * 获得某个job最后一次的构建结果
     *
     * @param jobName job的名称
     * @throws URISyntaxException
     * @throws IOException
     */
    public BuildResult getJobResult(String jobName) throws URISyntaxException, IOException {
        JenkinsServer jenkins = new JenkinsServer(new URI(uri), name, token);
        JobWithDetails jobWithDetails = jenkins.getJob(jobName);
        Build lastBuild = jobWithDetails.getLastBuild();
        BuildResult buildResult = lastBuild.details().getResult();
        return buildResult;
    }


    /**
     * 停止正在进行的构建。（这个构建必然是最新的一次构建）
     *
     * @param jobName
     * @throws IOException
     * @throws URISyntaxException
     */
    public static void stopJob(String jobName) throws IOException, URISyntaxException {
        JenkinsServer jenkins = new JenkinsServer(new URI(uri), name, token);
        JobWithDetails jobWithDetails = jenkins.getJob(jobName);
        Build lastBuild = jobWithDetails.getLastBuild();
        lastBuild.Stop();
    }


    /**
     * 运行一个项目的指定分支
     *
     * @param jobName
     * @param params  参数名称，这个要和job配合使用。
     *                比如，我们构建某个指定分支的情况。具体使用看单元测试。
     * @throws URISyntaxException
     * @throws IOException
     */
    public static void runJobWithParameter(String jobName, Map<String, String> params) {
        JenkinsServer jenkins = null;
        try {
            jenkins = new JenkinsServer(new URI(uri), name, token);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        try {
            jenkins.getJob(jobName).build(params);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 删除一个job
     *
     * @param jobName
     * @throws IOException
     */
    public void deleteJob(String jobName) throws IOException, URISyntaxException {
        JenkinsServer jenkins = new JenkinsServer(new URI(uri), name, token);
        jenkins.deleteJob(jobName);
    }

    /**
     * 获取某个job的日志。
     *
     * @param jobName
     * @param bufferOffset  日志的偏离量。
     * @return
     * @throws Exception
     */
    public static String getConsoleOutLog(String jobName, Integer bufferOffset) throws Exception {
        if (bufferOffset == null || bufferOffset < 0) {
            bufferOffset = 0;
        }
        JenkinsServer jenkinsServer = new JenkinsServer(new URI(uri), name, token);
        JobWithDetails jobWithDetails = jenkinsServer.getJob(jobName).details();
        ConsoleLog consoleLog = jobWithDetails.getLastBuild().details().getConsoleOutputText(bufferOffset);
        String consoleTxt = consoleLog.getConsoleLog();
        return consoleTxt;
    }


}

